import 'dart:ui';

import 'package:pui_flutter/src/constants/pui_constants.dart';

class PPMultiStateConfig {
  /// 状态
  PPMultiState state = PPMultiState.CONTENT;

  /// 图标
  String iconAssetsPath;

  /// assets资源包名
  String? package = PUIGlobalConstant.packageName;

  /// 状态标题
  String title;

  /// 操作按钮文案
  String operateText;

  /// 是否显示操作视图
  bool isShowOperateView;

  /// 按钮渐变色
  List<Color> buttonGradient = [
    const Color(0xFFFF8124),
    const Color(0xFFFF682F)
  ];

  static const List<Color> _defaultGradient = [
    Color(0xFFFF8124),
    Color(0xFFFF682F)
  ];

  PPMultiStateConfig({
    this.iconAssetsPath = PUIMultiStateLayoutConstant.defaultEmptyStateIconPath,
    this.title = PUIMultiStateLayoutConstant.defaultEmptyTitleText,
    this.operateText = PUIMultiStateLayoutConstant.defaultOperateText,
    this.isShowOperateView = false,
    this.buttonGradient = _defaultGradient,
  });

  PPMultiStateConfig.empty({
    this.iconAssetsPath = PUIMultiStateLayoutConstant.defaultEmptyStateIconPath,
    this.title = PUIMultiStateLayoutConstant.defaultEmptyTitleText,
    this.operateText = PUIMultiStateLayoutConstant.defaultOperateText,
    this.isShowOperateView = false,
    this.buttonGradient = _defaultGradient,
  }) : state = PPMultiState.EMPTY;

  PPMultiStateConfig.exception({
    this.iconAssetsPath =
        PUIMultiStateLayoutConstant.defaultExceptionStateIconPath,
    this.title = PUIMultiStateLayoutConstant.defaultExceptionTitleText,
    this.operateText = PUIMultiStateLayoutConstant.defaultOperateText,
    this.isShowOperateView = false,
    this.buttonGradient = _defaultGradient,
  }) : state = PPMultiState.EXCEPTION;

  PPMultiStateConfig.custom({
    this.iconAssetsPath =
        PUIMultiStateLayoutConstant.defaultCustomStateIconPath,
    this.title = PUIMultiStateLayoutConstant.defaultCustomTitleText,
    this.operateText = PUIMultiStateLayoutConstant.defaultOperateText,
    this.isShowOperateView = false,
    this.buttonGradient = _defaultGradient,
  }) : state = PPMultiState.CUSTOM;

  /// 合并拓展状态对象属性值到当前对象中
  PPMultiStateConfig merge(PPMultiStateConfig? other) {
    if (other == null || state != other.state) return this;
    if (other.iconAssetsPath.isNotEmpty &&
        other.iconAssetsPath != iconAssetsPath) {
      iconAssetsPath = other.iconAssetsPath;
      // 如果外部传入的 assets 相关资源不为空，则使用重置掉默认的packageName
      package = null;
    }
    if (other.title.isNotEmpty) {
      title = other.title;
    }
    if (other.operateText.isNotEmpty) {
      operateText = other.operateText;
    }
    isShowOperateView = other.isShowOperateView;
    buttonGradient = other.buttonGradient;
    return this;
  }

  /// 合并拓展状态对象属性值到当前对象中
  PPMultiStateConfig mergeBy(String? iconAssetsPath, String? title,
      String? operateText, bool? isShowOperateView) {
    if (iconAssetsPath != null &&
        iconAssetsPath.isNotEmpty &&
        iconAssetsPath != this.iconAssetsPath) {
      this.iconAssetsPath = iconAssetsPath;
      // 如果外部传入的 assets 相关资源不为空，则使用重置掉默认的packageName
      package = null;
    }
    if (title != null && title.isNotEmpty) {
      this.title = title;
    }
    if (operateText != null && operateText.isNotEmpty) {
      this.operateText = operateText;
    }
    if (isShowOperateView != null) {
      this.isShowOperateView = isShowOperateView;
    }
    return this;
  }
}

/// 多状态枚举类型，用于 controller 控制状态切换
enum PPMultiState {
  /// 内容状态，显示外部提供的 child widget
  CONTENT,

  /// 加载中状态
  LOADING,

  /// 空状态
  EMPTY,

  /// 异常状态
  EXCEPTION,

  /// 其他自定义业务状态，如：未登录等
  CUSTOM,
}
